#include "effects.h"
#include "led_driver.h"

// 静态颜色效果实现
StaticColorEffect::StaticColorEffect(uint8_t red, uint8_t green, uint8_t blue) 
    : ledDriver(nullptr), r(red), g(green), b(blue) {
}

void StaticColorEffect::init(LEDDriver* driver) {
    ledDriver = driver;
    // 设置所有LED为指定颜色
    ledDriver->setAllPixels(r, g, b);
    ledDriver->show();
}

void StaticColorEffect::update() {
    // 静态效果需要持续刷新以保证颜色正确显示
    if (ledDriver) {
        ledDriver->setAllPixels(r, g, b);
        ledDriver->show();
    }
}

const char* StaticColorEffect::getName() const {
    return "Static Color";
}

// 呼吸灯效果实现
BreathingEffect::BreathingEffect(uint8_t red, uint8_t green, uint8_t blue)
    : ledDriver(nullptr), r(red), g(green), b(blue), brightness(0.0f), direction(1) {
}

void BreathingEffect::init(LEDDriver* driver) {
    ledDriver = driver;
}

void BreathingEffect::update() {
    if (ledDriver) {
        // 使用正弦函数实现平滑的呼吸效果
        // 2秒完成一个完整周期(上升+下降)
        static unsigned long lastUpdate = 0;
        unsigned long currentTime = millis();
        
        // 每10ms更新一次
        if (currentTime - lastUpdate >= 10) {
            lastUpdate = currentTime;
            
            // 计算角度(0-2π对应2秒)
            static float angle = 0.0f;
            angle += (2.0f * PI) / 200.0f; // 200个10ms步长完成2秒周期
            if (angle >= 2.0f * PI) {
                angle = 0.0f;
            }
            
            // 使用正弦函数计算亮度(0.0-1.0)
            brightness = (sin(angle - PI/2) + 1.0f) / 2.0f;
            
            // 计算实际颜色值
            uint8_t actualR = (uint8_t)(r * brightness);
            uint8_t actualG = (uint8_t)(g * brightness);
            uint8_t actualB = (uint8_t)(b * brightness);
            
            // 设置所有LED颜色
            ledDriver->setAllPixels(actualR, actualG, actualB);
            ledDriver->show();
        }
    }
}

const char* BreathingEffect::getName() const {
    return "Breathing";
}